package puppy.query;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;

import util.nlp.SimpleTrie;

public class QueryNGramExtractor {

	
	Hashtable<String,? extends Number> query =null;
	
	private SimpleTrie trie = new SimpleTrie();
	
	private SimpleTrie trie_reverse = new SimpleTrie();

    
	public void addItem(String string) {

		
	//	hash.put(string, string);
		trie.add(string);
		
		String reverse= reverseString(string);
		
		trie_reverse.add(reverse);

	}
	
	
	
	public String reverseString(String s){
		String t= s;
	    t = t.replace("_", " ");
		String tt[] = t.split("\\s+");
		t="";
		for(int i =tt.length-1; i>=0; i--){
			t= t+ tt[i]+ " ";		
		}
		
		t=t.trim().replace(" ", "_");
		return t.trim();
		
	}
	
	/*
	 *  get a list of tag matches in the following style
	 *	  
	 * star_wars --> star_wars_legos, star_wars_games, games_star_wars, etc..
	 *  
	 *  includes cases as:
	 *  
	 *  star_wars_games -->   star_wars_lego , ..
	 *  
	 *  
	 */
	
	
	
	public HashSet<String> getDictionaryMatches(String s){
		
		HashSet<String> g= new HashSet<String>();
		
		
		
		String t[]= trie.suggest(s);
		for(int i=0;t!=null && i<t.length;i++){
			if(!t[i].equals(s)){
				
				if(t[i].length()> s.length()){
				if(t[i].contains("_"+s+"_")|| t[i].endsWith("_"+s) ||t[i].startsWith(s+"_") ){
					g.add(t[i]);
				}
				}
			else{
				
				if(s.contains("_"+t[i]+"_")|| s.endsWith("_"+t[i])|| s.startsWith(t[i]+"_")){
					g.add(t[i]);
				}
				
				
			}
			}
			//System.out.println("normaL:" + t[i] + "\t"+ s);
		}
		
	
		//adding reverse matces (suffix matching)
		String reverse = reverseString(s);
		
		String tt[] = trie_reverse.suggest(reverse);
		for(int i=0; tt!=null && i < tt.length; i++){
			String t_r = reverseString(tt[i]);
			//System.out.println("reverse\t"+t_r + "\t"+ s);
			if(!t_r.equals(s)){
				if(t_r.length()>s.length()){
					
					if(t_r.contains("_"+s+"_")|| t_r.endsWith("_"+s) || t_r.startsWith(s+"_")){
						g.add(t_r);
					}
				}else{
					
					if(s.contains("_"+t_r+"_")|| s.endsWith("_"+t_r)|| s.startsWith(t_r+"_")){
						g.add(t_r);
					}
					
				}
				
				
			}
		}
		
		return g;
		
	}
	
	
	public HashSet<String> getTagMatches(String tag,int minimum_length, boolean only_original){
		
		HashSet<String> matches = new HashSet<String>();

		matches.addAll(getDictionaryMatches(tag));
		
		if(only_original){

			return matches;
		}
		
		//split the tag in n-grams, with minimum length specified by the parameter
		ArrayList<String> candidates = getMatches(tag, minimum_length);
	
		for(int i=0;i < candidates.size(); i++){
			HashSet<String> temp = getDictionaryMatches(candidates.get(i));
			
			matches.addAll(temp);
			
		}
		return matches;
		
	}
	
	
	

	public ArrayList<String> getMatches(String tag, int minimum_length) {
		ArrayList<String> matches = new ArrayList<String>();

		tag = tag.replace("_", " ");
		String t[] = tag.split("\\s+");

		int div = minimum_length;
		while (div < t.length) {

			for (int offset = 0; offset < t.length; offset++) {
				String sub = "";
				int j=0;
				while(j<div && j + offset<t.length ){
					sub = sub + t[j+offset] + " ";
					j++;
				//	System.out.println("here " + i + "\tofsset:" +offset +"\t"+ div + "\t"+ t.length) ;
				}
				
			//	System.out.println("outside \tofsset:" +offset +"\t"+ div + "\t"+ t.length) ;
				
				if (!sub.equals("") && j>=div) {
					sub = sub.trim();
					sub  = sub.replace(" ","_");
					//System.out.println(sub + "--\t---" + tag);
					if(sub.length()>tag.length()){
						if(sub.contains(tag+ "_") || sub.contains("_"+tag)){
							matches.add(sub);
						}
						
					}else{
						
						if(tag.contains(sub+ "_") || tag.contains("_"+sub)){
							matches.add(sub);
						}
					//	matches.add(sub);
						
					}
				

				}
			}
			div++;
		}
		return matches;
	}
	
	
		public static void main(String args[]){
		
		String test = "star_wars";
		QueryNGramExtractor e= new QueryNGramExtractor();
		
		e.addItem("public_administration");
		e.addItem("star_wars_sites");
		e.addItem("star_wars_jedi_academy");
		
		e.addItem("star_thomson");
		e.addItem("wars_thomson");
		e.addItem("lego_");
		
		HashSet<String> set = e.getTagMatches(test,2,false);
		
		
		//e.getDictionaryMatches("wars111_buenas");
		Iterator<String> iter = set.iterator();
		
		while(iter.hasNext()){
			
			String t = iter.next();
			System.out.println(t);
			
		}
		
		//System.out.println(e.reverseString(test));
		e.getMatches(test,1);
		
		
	}
		
		public void delete(){
			
			
		//	trie.clean();
			//trie_reverse.clean();
			trie=null;
			trie_reverse=null;
			query.clear();
			query=null;
			
		}
	
	
}
